package cn.siriusbot.siriuspro.bot.plugin.database;

import cn.siriusbot.siriuspro.admin.dao.PluginsMapper;
import cn.siriusbot.siriuspro.admin.entity.Plugins;
import cn.siriusbot.siriuspro.bot.application.SiriusApplicationInfo;
import cn.siriusbot.siriuspro.bot.plugin.PlugInClient;
import cn.siriusbot.siriuspro.uitls.AppContextUtil;

public abstract class PlugInDatabaseInfo implements PlugInClient {

    PluginsMapper pluginsMapper;

    /**
     * 懒加载初始化
     */
    private void lazyLoading() {
        if (pluginsMapper == null) {
            pluginsMapper = AppContextUtil.getBean(PluginsMapper.class);
        }
    }

    /**
     * 更新数据到数据库
     */
    protected void updateInfo() {
        try {
            lazyLoading();
            SiriusApplicationInfo info = getInfo();
            Plugins plugins = pluginsMapper.queryByPackageName(info.getPackageName());
            if (plugins == null) {
                plugins = new Plugins(info);
                pluginsMapper.insert(plugins);
            }
        } catch (Throwable ignored) {

        }
    }

}
